%%%-----------------------
%%% 1. America
%%%------------------------

clear all; clc; 
load dataAm3.txt; load investAm.txt;
k=4; opt=2; nsim=100; mmax=5; 
gdp=[dataAm3(:,1:15) dataAm3(:,17:22)]; 
pop=[dataAm3(:,23:37) dataAm3(:,39:44)];
ter={[dataAm3(:,45:59) dataAm3(:,61:66)] [dataAm3(:,67:81) dataAm3(:,83:88)]...
    [dataAm3(:,89:103) dataAm3(:,105:110)]};
invst=investAm; % Nicaragua is not included. therefore we dropped it from dataAm3 above.

resultsAm={};

for i = 1:3   % 1 = total ter; 2 == domestic ter; 3 == transnational ter;
        for j = 1:2 % 1 = ter/1000; 2 = ter/pop;
                for m=1:2 % 1 = level ter; 2 = 1st differenced ter;
                    [yy, ylag, tlag, ws, wn]=prepdat4(gdp, ter{i}, invst, pop, j, m);
                    [T N]=size(yy);
                    xx = [tlag ws wn];
                    [bfinal1 pval mpy1 mpx mpu1 converg1] = fapr1(yy, ylag, xx, opt, nsim, mmax, k, N);
             
                    resultsAm.beta{i,j,m} = bfinal1;
                    resultsAm.pval{i,j,m} = pval;
                    resultsAm.stat{i,j,m} = [mpy1 mpx mpu1 converg1];
                    %keep resultsAm dataAm3 investAm k opt nsim mmax ter gdp invst pop i j m
                end
        end
end

keep resultsAm;


%%%------------------
%%% 2. Asia 
%%%--------------------

clc; load dataAs3.txt; load investAs.txt; 
k=4; opt=2; nsim=100; mmax=5;  
gdp={dataAs3(:,1:21) dataAs3(:,1:21) [dataAs3(:,1:9) dataAs3(:,11:21)]}; % drop Macau for iterate
pop={dataAs3(:,22:42) dataAs3(:,22:42) [dataAs3(:,22:30) dataAs3(:,32:42)]}; 
ter={dataAs3(:,43:63) dataAs3(:,64:84) [dataAs3(:,85:93) dataAs3(:,95:105)]};
invst={investAs(:,1:21) investAs(:,1:21) [investAs(:,1:9) investAs(:,11:21)]};

resultsAs={};

for i = 1:3   % 1 = total ter; 2 == domestic ter; 3 == transnational ter;
        for j = 1:2 % 1 = ter/1000; 2 = ter/pop;
                for m=1:2 % 1 = level ter; 2 = 1st differenced ter;
                    [yy, ylag, tlag, ws, wn]=prepdat4(gdp{i}, ter{i}, invst{i}, pop{i}, j, m);
                    [T N]=size(yy);
                    xx = [tlag ws wn];
                    [bfinal1 pval mpy1 mpx mpu1 converg1] = fapr1(yy, ylag, xx, opt, nsim, mmax, k, N);
             
                    resultsAs.beta{i,j,m} = bfinal1;
                    resultsAs.pval{i,j,m} = pval;
                    resultsAs.stat{i,j,m} = [mpy1 mpx mpu1 converg1];
                    %keep resultsAs dataAs3 investAs k opt nsim mmax ter gdp invst pop i j m
                end
        end
end

keep resultsAm resultsAs;


%%%---------------------------------
%%% 3. Middle Eeast and North Africa
%%%----------------------------------

clc; load dataMe3.txt; load investMe.txt;  
k=4; opt=2; nsim=200; mmax=5;  
gdp={dataMe3(:,1:13) dataMe3(:,1:13) [dataMe3(:,1:9) dataMe3(:,11:13)]}; % drop Morocco for iterate
pop={dataMe3(:,14:26) dataMe3(:,14:26) [dataMe3(:,14:22) dataMe3(:,24:26)]}; 
ter={dataMe3(:,27:39) dataMe3(:,40:52) [dataMe3(:,53:61) dataMe3(:,63:65)]};
invst={investMe(:,1:13) investMe(:,1:13) [investMe(:,1:9) investMe(:,11:13)]};

resultsMe={};

for i = 1:3   % 1 = total ter; 2 == domestic ter; 3 == transnational ter;
        for j = 1:2 % 1 = ter/1000; 2 = ter/pop;
                for m=1:2 % 1 = level ter; 2 = 1st differenced ter;
                    [yy, ylag, tlag, ws, wn]=prepdat4(gdp{i}, ter{i}, invst{i}, pop{i}, j, m);
                    [T N]=size(yy);
                    xx = [tlag ws wn];
                    [bfinal1 pval mpy1 mpx mpu1 converg1] = fapr1(yy, ylag, xx, opt, nsim, mmax, k, N);
             
                    resultsMe.beta{i,j,m} = bfinal1;
                    resultsMe.pval{i,j,m} = pval;
                    resultsMe.stat{i,j,m} = [mpy1 mpx mpu1 converg1];
                    %keep resultsMe dataMe3 investMe k opt nsim mmax ter gdp invst pop i j m
                end
        end
end

keep resultsAm resultsAs resultsMe;


%%%------------------------------
%%% 4. sub-Saharan Africa
%%%------------------------------

clc; load dataAf3.txt; load investAf.txt;
k=4; opt=2; nsim=100; mmax=5;
gdp=[dataAf3(:,1:16) dataAf3(:,18:24)]; pop=[dataAf3(:,25:40) dataAf3(:,42:48)];
ter={[dataAf3(:,49:64) dataAf3(:,66:72)] [dataAf3(:,73:88) dataAf3(:,90:96)]... 
    [dataAf3(:,97:112) dataAf3(:,114:120)]};
invst=investAf; % Sierra Leone is not included. therefore we dropped it from dataAf3 above.

resultsAf={};

for i = 1:3   % 1 = total ter; 2 == domestic ter; 3 == transnational ter;
        for j = 1:2 % 1 = ter/1000; 2 = ter/pop;
                for m=1:2 % 1 = level ter; 2 = 1st differenced ter;
                    [yy, ylag, tlag, ws, wn]=prepdat4(gdp, ter{i}, invst, pop, j, m);
                    [T N]=size(yy);
                    xx = [tlag ws wn];
                    [bfinal1 pval mpy1 mpx mpu1 converg1] = fapr1(yy, ylag, xx, opt, nsim, mmax, k, N);
             
                    resultsAf.beta{i,j,m} = bfinal1;
                    resultsAf.pval{i,j,m} = pval;
                    resultsAf.stat{i,j,m} = [mpy1 mpx mpu1 converg1];
                    %keep resultsAf dataAf3 investAf k opt nsim mmax ter gdp invst pop i j m
                end
        end
end

keep resultsAm resultsAs resultsMe resultsAf;


%%%--------------------------
%%% 5. Western Europe
%%%--------------------------

clc; load dataEu3.txt; load investEu.txt;  
k=4; opt=2; nsim=100; mmax=5;
% drop Iceland for GTD dom, and Finland for Iterate
gdp={dataEu3(:,1:19) [dataEu3(:,1:8) dataEu3(:,10:19)] [dataEu3(:,1:3) dataEu3(:,5:19)]};
pop={dataEu3(:,20:38) [dataEu3(:,20:27) dataEu3(:,29:38)] [dataEu3(:,20:22) dataEu3(:,24:38)]}; 
ter={dataEu3(:,39:57) [dataEu3(:,58:65) dataEu3(:,67:76)] [dataEu3(:,77:79) dataEu3(:,81:95)]};
invst={investEu(:,1:19) [investEu(:,1:8) investEu(:,10:19)] [investEu(:,1:3) investEu(:,5:19)]};

resultsEu={};

for i = 1:3   % 1 = total ter; 2 == domestic ter; 3 == transnational ter;
        for j = 1:2 % 1 = ter/1000; 2 = ter/pop;
                for m=1:2 % 1 = level ter; 2 = 1st differenced ter;
                    [yy, ylag, tlag, ws, wn]=prepdat4(gdp{i}, ter{i}, invst{i}, pop{i}, j, m);
                    [T N]=size(yy);
                    xx = [tlag ws wn];
                    [bfinal1 pval mpy1 mpx mpu1 converg1] = fapr1(yy, ylag, xx, opt, nsim, mmax, k, N);
             
                    resultsEu.beta{i,j,m} = bfinal1;
                    resultsEu.pval{i,j,m} = pval;
                    resultsEu.stat{i,j,m} = [mpy1 mpx mpu1 converg1];
                    %keep resultsEu dataEu3 investEu k opt nsim mmax ter gdp invst pop i j m
                end
        end
end

keep resultsAm resultsAs resultsMe resultsAf resultsEu;



%%%%%-------------------------------------------------
%%%%%       RESULTS
%%%%%-------------------------------------------------


%%%% Results cell arrays are as follow:

%{1,1,1} = Total terrorism/1000 level
%{1,1,2} = Total terrorism/1000 first-differenced
%{1,2,1} = Total terrorism/pop level
%{1,2,2} = Total terrorism/pop first-differenced

%{2,1,1} = Domestic terrorism/1000 level
%{2,1,2} = Domestic terrorism/1000 first-differenced
%{2,2,1} = Domestic terrorism/pop level
%{2,2,2} = Domestic terrorism/pop first-differenced

%{3,1,1} = Transnat terrorism/1000 level
%{3,1,2} = Transnat terrorism/1000 first-differenced
%{3,2,1} = Transnat terrorism/pop level
%{3,2,2} = Transnat terrorism/pop first-differenced


%%%% Table 4: only results shown in Table 4

Table4_Am=[resultsAm.beta{1,1,1}(2) resultsAm.beta{2,1,1}(2) resultsAm.beta{3,1,1}(2); ...
    resultsAm.beta{1,2,1}(2) resultsAm.beta{2,2,1}(2) resultsAm.beta{3,2,1}(2); ... 
    resultsAm.beta{1,1,2}(2) resultsAm.beta{2,1,2}(2) resultsAm.beta{3,1,2}(2); ...
    resultsAm.beta{1,2,2}(2) resultsAm.beta{2,2,2}(2) resultsAm.beta{3,2,2}(2)]; 

Table4_As=[resultsAs.beta{1,1,1}(2) resultsAs.beta{2,1,1}(2) resultsAs.beta{3,1,1}(2); ...
    resultsAs.beta{1,2,1}(2) resultsAs.beta{2,2,1}(2) resultsAs.beta{3,2,1}(2); ... 
    resultsAs.beta{1,1,2}(2) resultsAs.beta{2,1,2}(2) resultsAs.beta{3,1,2}(2); ...
    resultsAs.beta{1,2,2}(2) resultsAs.beta{2,2,2}(2) resultsAs.beta{3,2,2}(2)]; 

Table4_Me=[resultsMe.beta{1,1,1}(2) resultsMe.beta{2,1,1}(2) resultsMe.beta{3,1,1}(2); ...
    resultsMe.beta{1,2,1}(2) resultsMe.beta{2,2,1}(2) resultsMe.beta{3,2,1}(2); ... 
    resultsMe.beta{1,1,2}(2) resultsMe.beta{2,1,2}(2) resultsMe.beta{3,1,2}(2); ...
    resultsMe.beta{1,2,2}(2) resultsMe.beta{2,2,2}(2) resultsMe.beta{3,2,2}(2)];

Table4_Af=[resultsAf.beta{1,1,1}(2) resultsAf.beta{2,1,1}(2) resultsAf.beta{3,1,1}(2); ...
    resultsAf.beta{1,2,1}(2) resultsAf.beta{2,2,1}(2) resultsAf.beta{3,2,1}(2); ... 
    resultsAf.beta{1,1,2}(2) resultsAf.beta{2,1,2}(2) resultsAf.beta{3,1,2}(2); ...
    resultsAf.beta{1,2,2}(2) resultsAf.beta{2,2,2}(2) resultsAf.beta{3,2,2}(2)]; 

Table4_Eu=[resultsEu.beta{1,1,1}(2) resultsEu.beta{2,1,1}(2) resultsEu.beta{3,1,1}(2); ...
    resultsEu.beta{1,2,1}(2) resultsEu.beta{2,2,1}(2) resultsEu.beta{3,2,1}(2); ... 
    resultsEu.beta{1,1,2}(2) resultsEu.beta{2,1,2}(2) resultsEu.beta{3,1,2}(2); ...
    resultsEu.beta{1,2,2}(2) resultsEu.beta{2,2,2}(2) resultsEu.beta{3,2,2}(2)];


Table4_Level_TER1000=[Table4_Am(1,1:3); Table4_As(1,1:3); Table4_Me(1,1:3);...
    Table4_Af(1,1:3); Table4_Eu(1,1:3)]

Table4_Level_TERPOP=[Table4_Am(2,1:3); Table4_As(2,1:3); Table4_Me(2,1:3); ...
    Table4_Af(2,1:3); Table4_Eu(2,1:3)]

Table4_FD_TER1000=[Table4_Am(3,1:3); Table4_As(3,1:3); Table4_Me(3,1:3); ...
    Table4_Af(3,1:3); Table4_Eu(3,1:3)]

Table4_FD_TERPOP=[Table4_Am(4,1:3); Table4_As(4,1:3); Table4_Me(4,1:3); ...
    Table4_Af(4,1:3); Table4_Eu(4,1:3)]





